package handler;



import com.alibaba.fastjson.JSONObject;
import com.tydic.open.DopApiException;
import com.tydic.open.DopApiV2;

import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * @ClassName AbilityImpl
 * @Description TODO
 * @Author zxf
 * @DATE 2018/11/21
 */
public class AbilityImpl  {

    private static DopApiV2 sdk = new Login().login_dcoss();
    private Map<String,String> interface_map = new HashMap<>();
    private Map<String,String>  method_map = new HashMap<>();
    private static final Logger logger =  LogManager.getLogger(AbilityImpl.class);

    public AbilityImpl(){
        interface_map.put("维度信息查询","Api_Dim_info_n");
        interface_map.put("指标目录查询","Api_Target_define_n");
        interface_map.put("市场经营类_口径","Api_Target_busi_caliber_n");
        interface_map.put("市场经营类_结果","Api_Target_busi_prvnce_month_n");
        interface_map.put("收入类_口径","Api_Target_income_caliber_n");
        interface_map.put("收入类_结果","Api_Target_prvnce_month_n");
        interface_map.put("财务类_口径","Api_Target_finance_caliber_n");
        interface_map.put("财务类_结果","Api_Finance_prvnce_month_n");
        interface_map.put("人力资源类_口径","Api_Target_human_caliber_n");
        interface_map.put("人力资源类_结果","Api_Target_human_month_n");
        interface_map.put("网络发展投资类_口径","Api_Target_netdvlp_caliber_n");
        interface_map.put("网络发展投资类_结果","Api_Netdvlp_prvnce_Month_n");
        interface_map.put("网络运行类_口径","Api_Target_toperate_caliber_n");
        interface_map.put("网络运行类_结果","Api_Netoperate_prvnce_month_n");

        method_map.put("维度信息查询","api_dim_info_n");
        method_map.put("指标目录查询","api_target_define_n");
        method_map.put("市场经营类_口径","api_target_busi_caliber_n");
        method_map.put("市场经营类_结果","api_target_result_busi_prvnce_month_n");
        method_map.put("收入类_口径","api_target_income_caliber_n");
        method_map.put("收入类_结果","api_target_result_income_prvnce_month_n");
        method_map.put("财务类_口径","api_target_finance_caliber_n");
        method_map.put("财务类_结果","api_target_result_finance_prvnce_month_n");
        method_map.put("人力资源类_口径","api_target_human_caliber_n");
        method_map.put("人力资源类_结果","api_target_result_human_prvnce_month_n");
        method_map.put("网络发展投资类_口径","api_target_netdvlp_caliber_n");
        method_map.put("网络发展投资类_结果","api_target_result_netdvlp_one_prvnce_month_n");
        method_map.put("网络运行类_口径","api_target_netoperate_caliber_n");
        method_map.put("网络运行类_结果","api_target_result_netoperate_prvnce_month_n");





    }

    
    public String selectResult(String kpi_code, String month_id, String dim_value_name)  {
        Fastutil fastutil = new Fastutil();
        Map<String,String> params = new HashMap<>();
        String result_info = "";
        Map<String,Object>  result_map = new HashMap<>();
        logger.info("入参为："+kpi_code+" , "+month_id+" , "+dim_value_name+".");
      //查询目录
        params.put("usid", "JT_CAP");
        params.put("password", "jt810cap_123");
        params.put("method", method_map.get("指标目录查询"));
        params.put("kpi_code",kpi_code);
        String method = "GET";
        String kpi_info = doselect(interface_map.get("指标目录查询"),params,method);
        
        if(StringUtils.isBlank(kpi_info) || "false".equals(kpi_info)){
            result_map.put("result", "不存在该指标！");
            logger.info(kpi_code+": 未找到该指标");
            return FastJsonUtils.obj2json(result_map);
        }
        
        
        ArrayList<JSONObject> kpi_info_list = (ArrayList<JSONObject>) fastutil.analysis_data(kpi_info);
        if (null == kpi_info_list || kpi_info_list.size() > 1 ){
        	  result_map.put("result", "查询出零个或多个指标");
        	  logger.info(kpi_code+":查询出零个或多个指标。");
              return FastJsonUtils.obj2json(result_map);
        }

        //查询口径
        String busi_type = kpi_info_list.get(0).getString("busi_type");
        params.put("method",method_map.get(busi_type+"_口径"));
        String cliber_info = doselect(interface_map.get(busi_type+"_口径"),params,method);
        if(StringUtils.isBlank(cliber_info) || "false".equals(cliber_info)){
        	 result_map.put("result", "口径信息查询异常");
        	 logger.info(kpi_code + "：未返回口径信息"); 
             return FastJsonUtils.obj2json(result_map);
        }
        ArrayList<JSONObject>  cliber_info_list = (ArrayList<JSONObject>) fastutil.analysis_data(cliber_info);
  
        Collections.sort(cliber_info_list, new Comparator<JSONObject>() {

			@Override
			public int compare(JSONObject o1, JSONObject o2) {
				  if (o1.getString("dim_code").hashCode() > o2.getString("dim_code").hashCode()){
	                    return 1;
	                }
	                return 0;
			}
		});
        for (JSONObject jsonObject :cliber_info_list){


            params.put("dim_code",jsonObject.getString("dim_code"));
            params.put("method",method_map.get("维度信息查询"));

            String dim_info = doselect(interface_map.get("维度信息查询"),params,method);
            if(StringUtils.isBlank(dim_info) || "false".equals(dim_info)){
            	result_map.put("result", "维度信息查询失败");
            	logger.info(kpi_code + ",dim_code = "+jsonObject.getString("dim_code")+": 未查到维度信息。");
                return FastJsonUtils.obj2json(result_map);
            }
//            params.remove("dim_code");
            ArrayList<JSONObject> dim_info_list = (ArrayList<JSONObject>) fastutil.analysis_data(dim_info);
            List<JSONObject>  result_list = new ArrayList<>();
            for (JSONObject jsonObject_dim:dim_info_list){
            	 
            	if (jsonObject_dim.getString("dim_value_name").contains(dim_value_name)){
                    params.put("dim_value_code",jsonObject_dim.getString("dim_value_code"));
                    /*if("".equals(params.get("dim_value_code"))){
                    	result_map.put("result", "没有找到dim_value_code");
                        return FastJsonUtils.obj2json(result_map);
                    }*/
                    params.put("comp_id",jsonObject.getString("comp_id"));
                    params.put("method",method_map.get(busi_type+"_结果"));
                    params.put("month_id",month_id);
                   
                    result_info = doselect(interface_map.get(busi_type+"_结果"),params,method);
                    if(StringUtils.isBlank(result_info) || "false".equals(result_info)){
                    	result_map.put("result", "指标结果数据异常");
                    	logger.info(kpi_code +",dim_value_code ="+jsonObject_dim.getString("dim_value_code")+ ":查询结果为空或出现异常。");
                        return FastJsonUtils.obj2json(result_map);
                    }
                    JSONObject result = fastutil.analysis_result(result_info,jsonObject_dim.getString("dim_value_name"));
                    if(null != result) {
                    	result_list.add(result);
                    }
                }
                
                
            }
            if(result_list.isEmpty()) {
            	result_map.put("result", "查询结果为空");
            }else {
            	result_map.put("result", result_list);
            }
            

           
        }
       
       
        return FastJsonUtils.obj2json(result_map);
    }
    
    public String selectCaliber(String kpi_code) {
        Fastutil fastutil = new Fastutil();
        Map<String,String> params = new HashMap<>();
        Map<String,Object>  result_map = new HashMap<>();

        //查询目录
        params.put("usid", "JT_CAP");
        params.put("password", "jt810cap_123");
        params.put("method", method_map.get("指标目录查询"));
        params.put("kpi_code",kpi_code);
        String method = "GET";
        String kpi_info = doselect(interface_map.get("指标目录查询"),params,method);
        if(StringUtils.isBlank(kpi_info) || "false".equals(kpi_info)){
            
        	result_map.put("result","未查到该指标！" );
        	logger.info(kpi_code+": 未找到该指标");
        	return FastJsonUtils.obj2json(result_map);

        }
        ArrayList<JSONObject> kpi_info_list = (ArrayList<JSONObject>) fastutil.analysis_data(kpi_info);
        if (null == kpi_info_list || kpi_info_list.size() > 1){
        	result_map.put("result","数据异常" );
        	logger.info(kpi_code+":查询出零个或多个指标。");
        	return FastJsonUtils.obj2json(result_map);
        }

        //查询口径
        String busi_type = kpi_info_list.get(0).getString("busi_type");
        params.put("method",method_map.get(busi_type+"_口径"));
        String cliber_info = doselect(interface_map.get(busi_type+"_口径"),params,method);
        if(StringUtils.isBlank(cliber_info) || "false".equals(cliber_info)){
        	result_map.put("result","未查到口径信息" );
        	 logger.info(kpi_code + "：未返回口径信息"); 
        	return FastJsonUtils.obj2json(result_map);
        }
        ArrayList<JSONObject>  cliber_info_list = (ArrayList<JSONObject>) fastutil.analysis_data(cliber_info);
        result_map.put("result",cliber_info_list );
    	return FastJsonUtils.obj2json(result_map);
    }


    public  static String doselect(String interface_name,Map<String,String> params,String method){
        String path = "/dcoos/"+interface_name;
        String result = "false";
        params.remove("seqid");
        params.remove("timestamp");
        params.remove("sign");
        try {
            result = sdk.decryptApi(path, params);
        }catch (DopApiException e){
            e.printStackTrace();
        }
        return result;
    }


}
